/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

#include <thrust/iterator/counting_iterator.h>
#include <thrust/pending/limits.hpp>

namespace thrust {

  //======================================================
  // 

  template <class IntegerType, class SpaceTag>
  struct integer_range {
  
    typedef thrust::counting_iterator<IntegerType,SpaceTag> iterator;
    typedef IntegerType size_type;
    
  protected:
    volatile IntegerType m_start, m_finish;
    
  public:
    __host__ __device__
    inline
    integer_range()
      : m_start(thrust::numeric_limits<size_type>::max())
      , m_finish(thrust::numeric_limits<size_type>::max())
    {}
    __host__ __device__
    inline
    integer_range( IntegerType start, IntegerType finish)
      : m_start( start), m_finish( finish)
    {} 
    
    __host__ __device__
    inline iterator
    begin() const 
    { 
      return iterator( m_start);
    }
    
    __host__ __device__
    inline iterator 
    end() const 
    { 
      return iterator( m_finish);
    }

    __host__ __device__
    inline thrust::pair<IntegerType,IntegerType>
    get()
    {
      return thrust::make_pair( m_start, m_finish);
    }
  
    __host__ __device__
    inline IntegerType& 
    finish()
    {
      return m_finish;
    }

    __host__ __device__
    inline void 
    set_begin( IntegerType start)
    {
      m_start =start;
    }

    __host__ __device__
    inline void 
    set_end( IntegerType finish)
    {
      m_finish =finish;
    }

    __host__ __device__
    inline void 
    set( IntegerType start, IntegerType finish)
    {
      m_start =start;
      m_finish = finish;
    }

    __host__ __device__
    inline void 
    inc()
    {
      ++m_start;
      ++m_finish;
    }

    __host__ __device__
    inline void
    dec( size_type value)
    {
      m_start  -=value;
      m_finish -=value;
    }

    __host__ __device__
    inline size_type 
    size() const
    { 
      return m_finish - m_start;
    }
    
    __host__ __device__
    inline bool
    empty() const
    { 
      return m_finish ==m_start;
    }
    
    __host__ __device__
    inline
    void swap( integer_range& x)
    {
      std::swap( m_start, x.m_start);
      std::swap( m_finish, x.m_finish);
    }
  };
  
  template <class IntegerType, class SpaceTag>
  __host__ __device__
  inline
  counting_iterator<IntegerType,SpaceTag>
  make_interger_range( IntegerType first, IntegerType last)
  {
    return integer_range<IntegerType,SpaceTag>( first, last);
  }
  
} // end namespace thrust
